package com.happy.action.user;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.web.struts2.Struts2Utils;

import com.core.orm.hibernate.SpringSecurityUtils;
import com.happy.entity.Ariticle;
import com.happy.entity.User;
import com.happy.entity.UserInfo;
import com.happy.services.AriticleServices;
import com.happy.services.ResourceServices;
import com.happy.services.UserInfoServices;
import com.happy.services.UserServices;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 用户action
 * 
 * @author Administrator
 */
public class Users extends ActionSupport {

	private static final long serialVersionUID = -8648600120804991034L;
	protected final Logger logger = LoggerFactory.getLogger(getClass());
	private List<User> userList = new ArrayList<User>();
	@Autowired
	private UserServices userServices;
	@Autowired
	private UserInfoServices userInfoServices;
	private UserInfo userInfo;
	private int id;
	private String fckContent = "";
	@Autowired
	ResourceServices resourceServices;
	@Autowired
	AriticleServices aServices;
    private String title;
	@Override
	public String execute() throws Exception {
		// Page<com.happy.entity.Resource> a=new
		// Page<com.happy.entity.Resource>(10,true);
		// System.out.println(resourceServices.getPage(a).getResult().size());
		logger.info("user action execute 方法");
      
		return SUCCESS;
	}

	public String index() throws Exception {
        
		return execute();
	}

	public String form() throws Exception {
		logger.info("用户注册 user action form");
		return "form";
	}

	public String preEdit() throws Exception {
		logger.info("当前要修改的用户编号是:" + id);
		userInfo = userInfoServices.getByUserId(id);
		if (userInfo == null) {
			userInfo = new UserInfo();
			userInfo.setUserId(id);
		}
		return "preEdit";
	}

	public String view() throws Exception {
		logger.info("当前要查看的用户编号是:" + id);
		userInfo = userInfoServices.getByUserId(id);
		return "view";
	}

	public String edit() throws Exception {
		logger.info("确定修改 user action edit ");
		id = userInfo.getUserId();
		userInfoServices.saveOrUpdate(userInfo);
		return "edit";
	}

	public String fcks() throws Exception {
		logger.info("fck editor 示例");
		return "fcks";
	}

	public String content() throws Exception {
		logger.info("fck editor的内容是"+fckContent);
		Ariticle ariticle=new Ariticle();
		ariticle.setContent(fckContent);
		ariticle.setStatus("1");
		ariticle.setTitle(title);
		ariticle.setUser_info(userInfoServices.getByUserId(userServices.getUserByName(SpringSecurityUtils.getCurrentUserName()).getId()));
		aServices.save(ariticle);
		return "content";
	}

	public String list() throws Exception {
		logger.info("用户列表展示 user action list");
		userList = userServices.getAll();
		return "list";
	}

	public List<User> getUserList() {
		return userList;
	}

	public void setUserList(List<User> userList) {
		this.userList = userList;
	}

	public UserServices getUserServices() {
		return userServices;
	}

	public void setUserServices(UserServices userServices) {
		this.userServices = userServices;
	}

	public UserInfo getUserInfo() {
		return userInfo;
	}

	public void setUserInfo(UserInfo userInfo) {
		this.userInfo = userInfo;
	}

	public UserInfoServices getUserInfoServices() {
		return userInfoServices;
	}

	public void setUserInfoServices(UserInfoServices userInfoServices) {
		this.userInfoServices = userInfoServices;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getFckContent() {
		return fckContent;
	}

	public void setFckContent(String fckContent) {
		this.fckContent = fckContent;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

}
